package com.ColonyCount.Activity;


import java.security.InvalidParameterException;

import com.ColonyCount.ColonyCountContext;
import com.ColonyCount.ColonyCountHeap;
import com.ColonyCount.R;
import com.ColonyCount.View.RegionOfInterestView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;

public class ROIActivity extends Activity implements DialogInterface.OnCancelListener  {

	ColonyCountContext cccontext;
    RegionOfInterestView roiView;
    
    final Handler mHandler = new Handler();
    Thread workerThread = null;

    ProgressDialog dialog;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ColonyCountContext.dumpActivityEvent(this, "ROIActivity.onCreate() start");

		setContentView(R.layout.regionofinterest);

		cccontext = ColonyCountContext.getSingleton();

		roiView = (RegionOfInterestView) findViewById(R.id.imageROIView);
		
		Button buttonThreshold = (Button) findViewById(R.id.buttonNext);
		buttonThreshold.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startWorkerThread();
			}
		});
		
    	ColonyCountContext.dumpActivityEvent(this, "ROIActivity.onCreate() end");
	}
	
    @Override
	protected void onResume() {
    	ColonyCountContext.dumpActivityEvent(this, "ROIActivity.onResume() start");
    	super.onResume();

    	//cccontext.createSourceBitmap(this);
		roiView.setup(ColonyCountHeap.getLumaBitmap(), cccontext.getClipBounds(), cccontext.getThreshold());
		
    	ColonyCountContext.dumpActivityEvent(this, "ROIActivity.onResume() end");
    }
	
    @Override
	protected void onPause() {
    	ColonyCountContext.dumpActivityEvent(this, "ROIActivity.onPause() start");
		super.onPause();
		if(dialog != null)
		{
			dialog.dismiss();
		}

//		stopWorkerThread();
		removeHeapReferences();
		

		ColonyCountContext.dumpActivityEvent(this, "ROIActivity.onPause() end");
	}
    
    void removeHeapReferences()
    {
    	roiView.setup(null, null, 0);
    	System.gc();
    }
    
//    public synchronized void stopWorkerThread(){
//	  if(workerThread != null){
//	    Thread moribund = workerThread;
//	    workerThread = null;
//	    moribund.interrupt();
//	  }
//	}
  
    void startWorkerThread()
    {
		if(workerThread == null)
		{
			// Set region bounds
			cccontext.setClipBounds(roiView.getRegionBounds());
			
			// Remove reference to source bitmap so it can be released
			removeHeapReferences();
			
			// Start 
			dialog = ProgressDialog.show(ROIActivity.this, "Processing", "Creating Luma Map", true, true, this);
		    
			workerThread = new Thread() {
	            public void run() {
	            	
	            	cccontext.createLuma();
	            	
	            	mHandler.post(mWorkerThreadResults);
	            }
			};
			workerThread.start();
		};
    }
	
    final Runnable mWorkerThreadResults = new Runnable() {
    	public void run() {
    		dialog.dismiss();
    		workerThread = null;
    		
    		Intent intent = new Intent(ROIActivity.this, ThresholdActivity.class);
    		startActivity(intent);
    	}
    };

    @Override
    public void onCancel(DialogInterface arg0) 
    {
//    	stopWorkerThread();
    	if(workerThread == null)
    	{
        	finish();
    	}
    }	
}
